package com.github.catvod.spider.merge;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Stack;
import java.util.regex.Pattern;
import javax.annotation.Nullable;

/* loaded from: classes.dex */
public final class ZG {
    static final String[] h = {"", " ", "  ", "   ", "    ", "     ", "      ", "       ", "        ", "         ", "          ", "           ", "            ", "             ", "              ", "               ", "                ", "                 ", "                  ", "                   ", "                    "};
    private static Pattern V0 = Pattern.compile("^/((\\.{1,2}/)+)");
    private static final ThreadLocal<Stack<StringBuilder>> j = new Tw();

    class Tw extends ThreadLocal<Stack<StringBuilder>> {
        Tw() {
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // java.lang.ThreadLocal
        /* renamed from: h, reason: merged with bridge method [inline-methods] */
        public Stack<StringBuilder> initialValue() {
            return new Stack<>();
        }
    }

    public static class yh {
        final String V0;

        @Nullable
        StringBuilder h = ZG.V0();
        boolean j = true;

        public yh(String str) {
            this.V0 = str;
        }

        public String V0() {
            String t = ZG.t(this.h);
            this.h = null;
            return t;
        }

        public yh h(Object obj) {
            R2.u(this.h);
            if (!this.j) {
                this.h.append(this.V0);
            }
            this.h.append(obj);
            this.j = false;
            return this;
        }
    }

    public static boolean BQ(int i) {
        return i == 8203 || i == 173;
    }

    public static String F8(int i) {
        if (i < 0) {
            throw new IllegalArgumentException("width must be > 0");
        }
        String[] strArr = h;
        if (i < strArr.length) {
            return strArr[i];
        }
        int min = Math.min(i, 30);
        char[] cArr = new char[min];
        for (int i2 = 0; i2 < min; i2++) {
            cArr[i2] = ' ';
        }
        return String.valueOf(cArr);
    }

    public static boolean I(int i) {
        return i == 32 || i == 9 || i == 10 || i == 12 || i == 13 || i == 160;
    }

    public static String Qy(Collection<?> collection, String str) {
        return oq(collection.iterator(), str);
    }

    public static StringBuilder V0() {
        Stack<StringBuilder> stack = j.get();
        return stack.empty() ? new StringBuilder(8192) : stack.pop();
    }

    public static boolean W(String str) {
        if (str == null || str.length() == 0) {
            return false;
        }
        int length = str.length();
        for (int i = 0; i < length; i++) {
            if (!Character.isDigit(str.codePointAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static void h(StringBuilder sb, String str, boolean z) {
        int length = str.length();
        int i = 0;
        boolean z2 = false;
        boolean z3 = false;
        while (i < length) {
            int codePointAt = str.codePointAt(i);
            if (I(codePointAt)) {
                if ((!z || z2) && !z3) {
                    sb.append(' ');
                    z3 = true;
                }
            } else if (!BQ(codePointAt)) {
                sb.appendCodePoint(codePointAt);
                z2 = true;
                z3 = false;
            }
            i += Character.charCount(codePointAt);
        }
    }

    public static URL iU(URL url, String str) {
        if (str.startsWith("?")) {
            str = url.getPath() + str;
        }
        URL url2 = new URL(url, str);
        String replaceFirst = V0.matcher(url2.getFile()).replaceFirst("/");
        if (url2.getRef() != null) {
            replaceFirst = replaceFirst + "#" + url2.getRef();
        }
        return new URL(url2.getProtocol(), url2.getHost(), url2.getPort(), replaceFirst);
    }

    public static boolean j(String str, String... strArr) {
        for (String str2 : strArr) {
            if (str2.equals(str)) {
                return true;
            }
        }
        return false;
    }

    public static String n(String str, String str2) {
        try {
            try {
                return iU(new URL(str), str2).toExternalForm();
            } catch (MalformedURLException unused) {
                return new URL(str2).toExternalForm();
            }
        } catch (MalformedURLException unused2) {
            return "";
        }
    }

    public static boolean oC(String str) {
        if (str != null && str.length() != 0) {
            int length = str.length();
            for (int i = 0; i < length; i++) {
                if (!u(str.codePointAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }

    public static String oq(Iterator<?> it, String str) {
        if (!it.hasNext()) {
            return "";
        }
        String obj = it.next().toString();
        if (!it.hasNext()) {
            return obj;
        }
        yh yhVar = new yh(str);
        yhVar.h(obj);
        while (it.hasNext()) {
            yhVar.h(it.next());
        }
        return yhVar.V0();
    }

    public static boolean q(String str, String[] strArr) {
        return Arrays.binarySearch(strArr, str) >= 0;
    }

    public static String t(StringBuilder sb) {
        R2.u(sb);
        String sb2 = sb.toString();
        if (sb.length() > 8192) {
            sb = new StringBuilder(8192);
        } else {
            sb.delete(0, sb.length());
        }
        Stack<StringBuilder> stack = j.get();
        stack.push(sb);
        while (stack.size() > 8) {
            stack.pop();
        }
        return sb2;
    }

    public static boolean u(int i) {
        return i == 32 || i == 9 || i == 10 || i == 12 || i == 13;
    }
}
